package com.yida.gwj.vote.outside.controller;

import cn.hutool.core.date.DateUtil;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.Logical;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.dto.GwjGVoteProjectStatisticsDataDTO;
import com.yida.gwj.common.valid.Save;
import com.yida.gwj.common.valid.Submit;
import com.yida.gwj.common.valid.Update;
import com.yida.gwj.vote.outside.domain.GwjVoteProject;
import com.yida.gwj.vote.outside.domain.dto.GwjBaseBuildHouseOwnerDTO;
import com.yida.gwj.vote.outside.domain.dto.GwjEVoteProjectStatisticsDataDTO;
import com.yida.gwj.vote.outside.domain.dto.GwjVoteProjectDetailDTO;
import com.yida.gwj.vote.outside.domain.dto.GwjVoteProjectListDTO;
import com.yida.gwj.vote.outside.domain.request.GwjVoteProjectQueryRequest;
import com.yida.gwj.vote.outside.domain.request.GwjVoteProjectRequest;
import com.yida.gwj.vote.outside.enums.GwjVoteInitiateTypeEnum;
import com.yida.gwj.vote.outside.enums.GwjVoteStatusEnum;
import com.yida.gwj.vote.outside.service.IGwjBaseDataService;
import com.yida.gwj.vote.outside.service.IGwjVoteProjectService;
import com.yida.gwj.vote.outside.service.IGwjVoteStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lqq
 * @date 2023-03-10
 */
@Api(tags = "投票项目")
@RestController
@RequestMapping("/project")
public class GwjVoteProjectController extends BaseController {

    @Resource
    private IGwjVoteProjectService voteProjectService;

    @Resource
    private IGwjBaseDataService baseBuildService;

    @Resource
    private IGwjVoteStatisticsService voteProjectStatistics;

    @ApiOperation(value = "查询电子投票项目列表", response = GwjVoteProjectListDTO.class)
    @RequiresPermissions("gwj:vote:outside:e:project:list")
    @GetMapping("/e/list")
    public TableDataInfo eList(GwjVoteProjectQueryRequest request) {
        request.setInitiateType(GwjVoteInitiateTypeEnum.E_VOTE.getCode());
        startPage();
        List<GwjVoteProject> list = voteProjectService.selectVoteProjectList(request);
        list.forEach(p -> {
            if (p.getVoteStatus().equals(GwjVoteStatusEnum.VOTE_ING.getCode()) && DateUtil.compare(DateUtils.getNowDate(), p.getEndTime()) > 0) {
                p.setVoteStatus(GwjVoteStatusEnum.VOTE_END.getCode());
            }
        });
        return getDataDtoTable(list, GwjVoteProjectListDTO.class);
    }

    @ApiOperation(value = "查询公维金投票项目列表", response = GwjVoteProjectListDTO.class)
    @RequiresPermissions("gwj:vote:outside:g:project:list")
    @GetMapping("/g/list/{bizNum}")
    public TableDataInfo gList(@PathVariable Long bizNum) {
        startPage();
        List<GwjVoteProject> list = voteProjectService.selectVoteProjectList(new GwjVoteProjectQueryRequest() {{
            setBizNum(bizNum);
        }});
        list.forEach(p -> {
            if (p.getVoteStatus().equals(GwjVoteStatusEnum.VOTE_ING.getCode()) && DateUtil.compare(DateUtils.getNowDate(), p.getEndTime()) > 0) {
                p.setVoteStatus(GwjVoteStatusEnum.VOTE_END.getCode());
            }
        });
        list.removeIf(item -> item.getVoteStatus().equals(GwjVoteStatusEnum.CANCEL.getCode()));
        return getDataDtoTable(list, GwjVoteProjectListDTO.class);
    }

    @ApiOperation(value = "获取投票项目详细信息", response = GwjVoteProjectDetailDTO.class)
    @GetMapping(value = "/{voteId}")
    @RequiresPermissions("gwj:vote:outside:e:project:query")
    public AjaxResult getInfo(@PathVariable Long voteId) {
        return AjaxResult.success(voteProjectService.voteDetail(voteId));
    }

    @ApiOperation(value = "投票项目新增")
    @RequiresPermissions(value = {"gwj:vote:outside:e:project:add", "gwj:vote:outside:g:project:add"}, logical = Logical.OR)
    @Log(title = "公维金外网投票-新增", businessType = BusinessType.INSERT, systemName = "vote")
    @PostMapping("/add")
    public AjaxResult add(@Validated(Save.class) @RequestBody GwjVoteProjectRequest request) {
        //校验时间参数
        voteProjectService.checkTime(request);
        //查询基础数据楼栋、房屋、业主所有数据
        GwjBaseBuildHouseOwnerDTO dto = baseBuildService.getAllDataByBuildIds(request.getBuildIds(), request.getAreaId());
        dto.setCommunityId(request.getCommunityId());
        return AjaxResult.success(voteProjectService.addVote(request, dto));
    }

    @ApiOperation("电子投票项目修改")
    @RequiresPermissions("gwj:vote:outside:e:project:edit")
    @Log(title = "公维金外网投票-修改", businessType = BusinessType.UPDATE, systemName = "vote")
    @PutMapping("/update")
    public AjaxResult edit(@Validated(Update.class) @RequestBody GwjVoteProjectRequest request) {
        return toAjax(voteProjectService.updateOrSubmitVote(request, false));
    }

    @ApiOperation("电子投票项目提交")
    @RequiresPermissions("gwj:vote:outside:e:project:submit")
    @Log(title = "公维金外网投票-提交", businessType = BusinessType.UPDATE, systemName = "vote")
    @PutMapping("/submit")
    public AjaxResult submit(@Validated(Submit.class) @RequestBody GwjVoteProjectRequest request) {
        return toAjax(voteProjectService.updateOrSubmitVote(request, true));
    }

    @ApiOperation("投票项目取消投票")
    @RequiresPermissions(value = {"gwj:vote:outside:e:project:cancel", "gwj:vote:outside:g:project:cancel"}, logical = Logical.OR)
    @Log(title = "公维金外网投票-取消投票", businessType = BusinessType.DELETE, systemName = "vote")
    @DeleteMapping("/cancel/{voteId}")
    public AjaxResult cancel(@PathVariable Long voteId) {
        return toAjax(voteProjectService.voteCancel(voteId));
    }

    @ApiOperation("投票项目发起投票")
    @RequiresPermissions(value = {"gwj:vote:outside:e:project:start", "gwj:vote:outside:g:project:start"}, logical = Logical.OR)
    @Log(title = "公维金外网投票-发起投票", businessType = BusinessType.UPDATE, systemName = "vote")
    @GetMapping("/start/{voteId}")
    public AjaxResult start(@PathVariable Long voteId) {
        return toAjax(voteProjectService.voteStart(voteId));
    }

    @ApiOperation(value = "投票项目短信通知")
    @RequiresPermissions(value = {"gwj:vote:outside:e:project:sendSms", "gwj:vote:outside:g:project:sendSms"}, logical = Logical.OR)
    @Log(title = "公维金外网投票-短信通知", businessType = BusinessType.OTHER, systemName = "vote")
    @GetMapping("/smsNotification/{voteId}")
    public AjaxResult smsNotification(@PathVariable Long voteId) {
        return toAjax(voteProjectService.voteSmsNotification(voteId));
    }

    @ApiOperation(value = "电子投票项目图表显示", response = GwjEVoteProjectStatisticsDataDTO.class)
    @RequiresPermissions("gwj:vote:outside:e:project:statistics")
    @GetMapping("/e/statistics/{voteId}")
    public AjaxResult voteProjectStatistics(@PathVariable Long voteId) {
        return AjaxResult.success(new GwjEVoteProjectStatisticsDataDTO() {{
            //统计投票基础数据
            setBaseData(voteProjectStatistics.eVoteProjectStatisticsBaseData(voteId));
            //统计投票表决项数据
            setItemData(voteProjectStatistics.voteResultDetailStatistics(voteId));
        }});
    }

    @ApiOperation(value = "公维金投票图表显示", response = GwjGVoteProjectStatisticsDataDTO.class)
    @RequiresPermissions("gwj:vote:outside:g:project:statistics")
    @GetMapping("/g/statistics/{voteId}")
    public AjaxResult gVoteProjectStatistics(@PathVariable Long voteId) {
        return AjaxResult.success(voteProjectStatistics.gVoteProjectStatistics(voteId));
    }
}
